import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;

//基于 AtomicInteger 实现多线程自增同一个变量
public class Homework {
    private static AtomicInteger atomicInteger = new AtomicInteger(0);//定义原子类对象,且初始化为0

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            //实现+操作
            for (int i = 0; i <50000 ; i++) {
                atomicInteger.getAndIncrement();
            }
        });
        Thread t2 = new Thread(()->{
           //实现-操作
            for (int i = 0; i < 4000; i++) {
             atomicInteger.decrementAndGet();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("atomicInteger = "+ atomicInteger);//不用调用方法来获取值
    }
}
